/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.text.Collator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author damfercer
 */
@Entity
@Cacheable(false)
@Table(name = "town")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Town.findAll", query = "SELECT t FROM Town t"),
    @NamedQuery(name = "Town.findByIdTown", query = "SELECT t FROM Town t WHERE t.idTown = :idTown"),
    @NamedQuery(name = "Town.findByDescription", query = "SELECT t FROM Town t WHERE t.description = :description")})
public class Town implements Serializable, Comparable<Town> {
    @OneToMany(mappedBy = "town", fetch = FetchType.LAZY)
    private List<Company> companyList;
    @OneToMany(mappedBy = "residenceTown", fetch = FetchType.LAZY)
    private List<Search> searchList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_TOWN")
    private Integer idTown;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "DESCRIPTION")
    private String description;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "town", fetch = FetchType.LAZY)
    private List<Candidate> candidateList;
    @JoinColumn(name = "PROVINCE", referencedColumnName = "ID_PROVINCE")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Province province;

    public Town() {
    }

    public Town(Integer idTown) {
        this.idTown = idTown;
    }

    public Town(Integer idTown, String description) {
        this.idTown = idTown;
        this.description = description;
    }

    public Integer getIdTown() {
        return idTown;
    }

    public void setIdTown(Integer idTown) {
        this.idTown = idTown;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @XmlTransient
    public List<Candidate> getCandidateList() {
        return candidateList;
    }

    public void setCandidateList(List<Candidate> candidateList) {
        this.candidateList = candidateList;
    }

    public Province getProvince() {
        return province;
    }

    public void setProvince(Province province) {
        this.province = province;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idTown != null ? idTown.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Town)) {
            return false;
        }
        Town other = (Town) object;
        if ((this.idTown == null && other.idTown != null) || (this.idTown != null && !this.idTown.equals(other.idTown))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Town[ idTown=" + idTown + " ]";
    }

    @XmlTransient
    public List<Search> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<Search> searchList) {
        this.searchList = searchList;
    }

    @XmlTransient
    public List<Company> getCompanyList() {
        return companyList;
    }

    public void setCompanyList(List<Company> companyList) {
        this.companyList = companyList;
    }

    @Override
    public int compareTo(Town o) {
        return Collator.getInstance().compare(this.getDescription(), o.getDescription());
    }
    
}
